package org.springblade.minio.feign;


import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.minio.utils.MinioUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ZH
 */
@RestController
@AllArgsConstructor
public class MinioClientImpl implements MinioClient {
	private MinioUtils minioUtils;
	/**
	 * 获取文件下载连接
	 */
	@Override
	@PostMapping("/getDownloadUrl")
	public R<String> getDownloadLink(@ApiParam(value = "文件相对路径",required = true) @RequestParam String path) {
		String url = minioUtils.getUrlByObjectName(path);
		return R.data(url);
	}

	/**
	 * 上传文件
	 */
	@Override
	@PostMapping("/uploadToMinio")
	public R uploadToMinio(@ApiParam(value = "文件",required = true) @RequestParam MultipartFile multipartFile,
						   @ApiParam(value = "指定上传地址",required = true) @RequestParam String path) {
		try {
			minioUtils.uploadToMinio(multipartFile.getInputStream(),path);
			return R.success("ok");
		}catch (Exception e){
			return R.fail("file upload failed");
		}
	}

	/**
	 * 根据前缀删除文件
	 */
	@Override
	@PostMapping("/deletedFileByPrefix")
	public R deletedFileByPrefix(@ApiParam(value = "文件路径前缀",required = true) @RequestParam String prefix) {
		minioUtils.deleteObject(prefix);
		return R.success("ok");
	}
}
